package dao;

import java.util.ArrayList;

import org.hibernate.Session;

import metier.Personne;

public class DAOPersonneHib extends DAOHibernate implements DAOPersonne{

	public Personne get(int code) throws Exception {
		Personne	personne = null;
		Session	session = connect();		
		personne = (Personne) session.get(Personne.class, code);	
		close(session);
		return personne;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Personne> loadAll() throws Exception {
		ArrayList<Personne>	tab = null;
		Session session = connect();
		
		tab = (ArrayList<Personne>) session.createQuery("FROM Personne")
				.list();
		
		close(session);
		return tab;
	}
	
	

	@Override
	public void save(Personne personne) throws Exception {
		Session	session = connect();
		
		session.save(personne);
		
		close(session);	
		
		
	}

	@Override
	public void saveOrupdate(Personne personne) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Personne personne) throws Exception {
		Session	session = connect();
		
		session.delete(personne);
		personne.setId_personne(-1);
		
		close(session);
		
	}

	@Override
	public void update(Personne personne) throws Exception {
		Session	session = connect();
		
		session.update(personne);
		
		close(session);
		
		
	}

	@Override
	public void chiffreAffaire(String mois) throws Exception {
		Session session = connect();
		
	    session.createQuery("FROM Offre where date_depart");
	   //"SELECT * FROM offre where month(date_debut)=month(NOW())"
	   //SELECT COUNT(*) FROM offre where month(date_debut)=month(NOW()) 
				
		
		close(session);
		
	}

	@Override
	public Personne login(String pseudo, String mdp) throws Exception {
		Personne	p = null;
		Session session = connect();
		
		p =  (Personne) session.createQuery("FROM Personne where pseudo LIKE '" +pseudo+"'" + "AND mdp LIKE '" + mdp +"'").uniqueResult();
		
		close(session);
		return p;
	}

}
